﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Code_Generator
{
	class Program
	{
		const string ConnectionString = @"Data Source=localhost\SQLEXPRESS;Integrated Security=True;Database=LeadTracker";
		const string entityTargetFolder = @"C:\Source\Granite\LeadTracker\LeadTracker.Entities";
		const string repositoryTargetFolder = @"C:\Source\Granite\LeadTracker\LeadTracker.DataAccess";
		const string procTargetFolder = @"C:\Source\Granite\LeadTracker\LeadTracker Database\Schema Objects\Schemas\dbo\Programmability Generated";

		const string scratchFolder = @"C:\Source\Granite\Granite Code Generators\Scratch\";

		const string Namespace = @"LeadTracker.Models";
		const string DCNamespace = @"http://granite.codeplex.com/LeadTracker";

		static void Main(string[] args)
		{
			//PromptBuildDataObjects();
			GenerateEnums();
			GenerateEditor();
			GenerateLead();
			GenerateLeadComment();
		}

		private static void GenerateEnums()
		{
			var builder = new ModelBuilder();

			builder.Setup.ConnectionString = ConnectionString;
			builder.Setup.Namespace = Namespace;
			builder.Setup.DataContractNamespace = DCNamespace;
			builder.Setup.TargetFolder = scratchFolder;


			builder.Setup.TableName = "LeadStatus";
			builder.Setup.EnumDescription = "LeadStatus";
			builder.CreateEnum();

			builder.Setup.TableName = "LeadType";
			builder.Setup.EnumDescription = "LeadType";
			builder.CreateEnum();

		}



		private static void GenerateEditor()
		{
			var builder = new ModelBuilder();

			builder.Setup.ConnectionString = ConnectionString;
			builder.Setup.Namespace = Namespace;
			builder.Setup.DataContractNamespace = DCNamespace;
			builder.Setup.TargetFolder = scratchFolder;

			builder.Setup.TableName = "Editor";
			builder.Setup.TokenFields.Add("FirstName");
			builder.Setup.TokenFields.Add("LastName");
			builder.Setup.ToStringExpression = @"FirstName + "" "" + LastName";

			builder.CreateToken();
			builder.CreateModel();
			builder.CreateDTO();
			builder.CreateRepository();
			builder.CreateCollection();

			var procBuilder = new ProcBuilder(builder.Setup);
			procBuilder.CreateDeleteProc();
			procBuilder.CreateInsertModelProc();
			procBuilder.CreateSelectAllTokensProc();
			procBuilder.CreateSelectModelProc();
			procBuilder.CreateUpdateModelProc();

		}


		private static void GenerateLead()
		{
			var builder = new ModelBuilder();

			builder.Setup.ConnectionString = ConnectionString;
			builder.Setup.Namespace = Namespace;
			builder.Setup.DataContractNamespace = DCNamespace;
			builder.Setup.TargetFolder = scratchFolder;

			builder.Setup.TableName = "Lead";
			builder.Setup.TokenFields.Add("Title");

			builder.Setup.EnumFieldTypeMap["LeadStatusKey"] = "LeadStatus";
			builder.Setup.EnumFieldTypeMap["LeadTypeKey"] = "LeadType";

			builder.CreateToken();
			builder.CreateModel();
			//builder.CreateDTO();
			builder.CreateRepository();
			builder.CreateCollection();

			var procBuilder = new ProcBuilder(builder.Setup);
			procBuilder.CreateDeleteProc();
			procBuilder.CreateInsertModelProc();
			procBuilder.CreateSelectModelProc();
			procBuilder.CreateUpdateModelProc();

		}

		private static void GenerateLeadComment()
		{
			var builder = new ModelBuilder();

			builder.Setup.ConnectionString = ConnectionString;
			builder.Setup.Namespace = Namespace;
			builder.Setup.DataContractNamespace = DCNamespace;
			builder.Setup.TargetFolder = scratchFolder;

			builder.Setup.TableName = "LeadComment";
			builder.Setup.TokenFields.Add("Comment");
			builder.Setup.ToStringExpression = @"Comment";

			//builder.Setup.ParentColumn = "LeadKey";

			builder.CreateToken();
			builder.CreateModel();
			builder.CreateDTO();
			builder.CreateRepository();
			builder.CreateCollection();

			var procBuilder = new ProcBuilder(builder.Setup);
			procBuilder.CreateDeleteProc();
			procBuilder.CreateInsertModelProc();
			//procBuilder.CreateSelectAllTokensProc();
			procBuilder.CreateSelectModelProc();
			procBuilder.CreateUpdateModelProc();
			//procBuilder.CreateSelectModelByParentProc();

		}

		//	private static void PromptBuildDataObjects()
		//	{
		//		var builder = new ProcAndModelBuilder(ConnectionString);

		//		Console.Write("Schema name [dbo]? ");
		//		builder.SchemaName = ApplyDefault("dbo");

		//		Console.Write("Table name? ");
		//		builder.TableName = Console.ReadLine();

		//		Console.Write("Alternate Primary Column [none]? ");
		//		builder.AltPrimaryColumn = ApplyDefault("");

		//		Console.Write("Parent Column [none]? ");
		//		builder.ParentColumn = ApplyDefault("");

		//		Console.Write("Display Column for ToString() [none]? ");
		//		builder.DisplayColumn = ApplyDefault("");

		//
		//		if (builder.ParentColumn != "")
		//		{
		//			Console.Write("Alternate Parent Column [none]? ");
		//			builder.AltParentColumn = ApplyDefault("");
		//		}


		//		if (builder.ParentColumn == "")
		//			using (var con = new SqlConnection(ConnectionString))
		//			{
		//				con.Open();
		//				using (var cmd = new SqlCommand("SELECT COUNT(*) FROM " + builder.TableName, con))
		//				{
		//					builder.AllowSelectAll = (int)cmd.ExecuteScalar() < 250;
		//				}

		//				Console.Write("Allow Select All [" + (builder.AllowSelectAll ? "1" : "0") + "]? ");
		//				builder.AllowSelectAll = ApplyDefault(builder.AllowSelectAll);
		//			}

		//		builder.DataContractNamespace = DCNamespace;
		//		builder.Namespace = Namespace;


		//		builder.PopulateMetaData();
		//		builder.TargetFolder = procTargetFolder;

		//		builder.CreateSelectProc();
		//		builder.CreateInsertProc();
		//		builder.CreateDeleteProc();
		//		builder.CreateUpdateProc();

		//		builder.TargetFolder = entityTargetFolder;
		//		builder.CreateEntity();
		//		builder.CreateToken();

		//		builder.TargetFolder = repositoryTargetFolder;
		//		builder.CreateRepository();

		//	}

		static T ApplyDefault<T>(T b)
		{
			var a = Console.ReadLine();
			if (a != "")
				return ((dynamic)a);

			return b;
		}


	}

}


